package com.example;

import java.util.Scanner;

public class MainMenu {
    private static Scanner scanner = new Scanner(System.in);
    private static User currentUser;

    public static void main(String[] args) {
        while (true) {
            System.out.println("\n=== 宠物商店管理系统 ===");
            System.out.print("用户名：");
            String username = scanner.next();
            System.out.print("密码：");
            String password = scanner.next();

            try {
                currentUser = new UserService().login(username, password);
                if (currentUser != null) {
                    showMainMenu();
                } else {
                    System.out.println("登录失败，请检查用户名和密码！");
                }
            } catch (Exception e) {
                System.out.println("系统错误：" + e.getMessage());
            }
        }
    }

    private static void showMainMenu() throws Exception {
        while (true) {
            if (currentUser.isAdmin()) {
                System.out.println("\n[管理员菜单] 1.添加宠物 2.修改宠物 3.删除宠物 4.查看所有宠物 0.退出");
            } else {
                System.out.println("\n[用户菜单] 1.查看宠物 2.购买宠物 0.退出");
            }

            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    if (currentUser.isAdmin()) handleAddPet();
                    else handleViewPets();
                    break;
                case 2:
                    if (currentUser.isAdmin()) handleUpdatePet();
                    else handlePurchasePet();
                    break;
                case 3:
                    if (currentUser.isAdmin()) handleDeletePet();
                    break;
                case 4:
                    if (currentUser.isAdmin()) handleViewPets();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("无效选项！");
            }
        }
    }

    private static void handleAddPet() throws Exception {
        System.out.println("输入宠物信息：");
        Pet pet = new Pet();
        System.out.print("名称："); pet.setName(scanner.next());
        System.out.print("类型："); pet.setType(scanner.next());
        System.out.print("年龄："); pet.setAge(scanner.nextInt());
        System.out.print("性别："); pet.setGender(scanner.next());
        System.out.print("价格："); pet.setPrice(scanner.nextDouble());

        new PetService().addPet(pet);
        System.out.println("添加成功！");
    }

    private static void handlePurchasePet() throws Exception {
        System.out.print("输入要购买的宠物ID：");
        int petId = scanner.nextInt();
        new PetService().purchasePet(currentUser.getId(), petId);
        System.out.println("购买成功！");
    }
}
